﻿namespace DslEditorPowerToy.Controls.Languages.ToolWindows
{
    using DslEditorPowerToy.Controls;
    using DslEditorPowerToy.Controls.Languages;
    using Microsoft.VisualStudio.Modeling.Shell;
    using Microsoft.Win32;
    using System;
    using System.Windows.Forms;

    [CLSCompliant(false)]
    public abstract class ControlHostBase : DslLanguageBase
    {
        private ComponentSelectionBroker _selectionBroker;

        public ControlHostBase(IServiceProvider serviceProvider, System.Type dslLanguageDocViewType) : base(serviceProvider, dslLanguageDocViewType)
        {
        }

        private void _selectionBroker_EnvironmentSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.OnEnvironmentSelectionChanged(e.SelectionProvider, e.Source);
        }

        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    SystemEvents.UserPreferenceChanged -= new UserPreferenceChangedEventHandler(this.OnUserPreferenceChanged);
                    if (this._selectionBroker != null)
                    {
                        this._selectionBroker.EnvironmentSelectionChanged -= new SelectionChangedEventHandler(this._selectionBroker_EnvironmentSelectionChanged);
                        this._selectionBroker.Dispose();
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }

        protected override void OnDocumentWindowChanged(ModelingDocView oldView, ModelingDocView newView)
        {
            base.OnDocumentWindowChanged(oldView, newView);
            if (base.DocView != null)
            {
                this._selectionBroker.RegisterDocView(base.DocView);
            }
        }

        protected virtual void OnEnvironmentSelectionChanged(ISelectionProvider selectionProvider, SelectionChangeSource source)
        {
        }

        protected override void OnToolWindowCreate()
        {
            base.OnToolWindowCreate();
            this.SetControlUserPreferences();
            SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(this.OnUserPreferenceChanged);
            this._selectionBroker = new ComponentSelectionBroker(this, this.Control);
            this._selectionBroker.EnvironmentSelectionChanged += new SelectionChangedEventHandler(this._selectionBroker_EnvironmentSelectionChanged);
        }

        protected virtual void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
        {
            this.SetControlUserPreferences();
        }

        private void SetControlUserPreferences()
        {
            if (this.Control != null)
            {
                this.Control.Font = base.ToolWindowFont;
            }
        }

        protected abstract System.Windows.Forms.Control Control { get; }

        public override IWin32Window Window
        {
            get
            {
                return this.Control;
            }
        }

        public abstract override string WindowTitle { get; }
    }
}

